package org.jeecg.modules.gis.util.tile;

import org.jeecg.modules.gis.entity.TilePbf;
import org.jeecg.modules.gis.service.impl.PbfTileServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;
import java.nio.file.Files;
@Component
public class PbfUtil {
    @Autowired
   private PbfTileServiceImpl pbfTileServiceImpl;
   public void pbfTodb(String BASE_DIRECTORY_PATH, String tableName) throws Exception{
       File baseDirectory = new File(BASE_DIRECTORY_PATH);
       if (!baseDirectory.exists() || !baseDirectory.isDirectory()) {
           System.err.println("目录不存在: " + BASE_DIRECTORY_PATH);
           return;
       }
       for (File outerDir : baseDirectory.listFiles(File::isDirectory)) {
           String z = outerDir.getName();
           for (File innerDir : outerDir.listFiles(File::isDirectory)) {
               String x = innerDir.getName();
               for (File file : innerDir.listFiles((dir, name) -> name.endsWith(".pbf"))) {
                   String y = file.getName().replace(".pbf", "");
                   byte[] pbfData = Files.readAllBytes(file.toPath());
                   TilePbf tilePbf = new TilePbf();
                   tilePbf.setPbf(pbfData);
                    tilePbf.setZ(Integer.parseInt(z));
                     tilePbf.setX(Integer.parseInt(x));
                      tilePbf.setY(Integer.parseInt(y));
                   pbfTileServiceImpl.saveToTargetTable(tilePbf, tableName);
               }
           }
       }
    }
}
